using System;
using Csla;
using Ent.AccesoDatos;
using System.Collections.Generic;
using System.Data;
using EntGuayasSLA.BL.Listas.Criterias;

namespace EntGuayasSLA.BL
{
    [Serializable]
    public class CategoriaExpediente : BusinessBase<CategoriaExpediente>
    {
        #region Business Methods

        public static readonly PropertyInfo<Guid> IdProperty = RegisterProperty<Guid>(p => p.Id);
        public Guid Id
        {
            get { return GetProperty(IdProperty); }
            set { SetProperty(IdProperty, value); }
        }
        public static readonly PropertyInfo<string> ExpedienteIdProperty = RegisterProperty<string>(c => c.ExpedienteId);
        public string ExpedienteId
        {
            get { return GetProperty(ExpedienteIdProperty); }
            set { SetProperty(ExpedienteIdProperty, value); }
        }

        public static readonly PropertyInfo<string> CategoriaIdProperty = RegisterProperty<string>(c => c.CategoriaId);
        public string CategoriaId
        {
            get { return GetProperty(CategoriaIdProperty); }
            set { SetProperty(CategoriaIdProperty, value); }
        }

        public static readonly PropertyInfo<string> ItemIdProperty = RegisterProperty<string>(c => c.ItemId);
        public string ItemId
        {
            get { return GetProperty(ItemIdProperty); }
            set { SetProperty(ItemIdProperty, value); }
        }

        public static readonly PropertyInfo<string> CodigoProperty = RegisterProperty<string>(c => c.Codigo);
        public string Codigo
        {
            get { return GetProperty(CodigoProperty); }
            set { SetProperty(CodigoProperty, value); }
        }

        public static readonly PropertyInfo<string> NombreProperty = RegisterProperty<string>(c => c.Nombre);
        public string Nombre
        {
            get { return GetProperty(NombreProperty); }
            set { SetProperty(NombreProperty, value); }
        }

        public static readonly PropertyInfo<string> OtroProperty = RegisterProperty<string>(c => c.Otro);
        public string Otro
        {
            get { return GetProperty(OtroProperty); }
            set { SetProperty(OtroProperty, value); }
        }

        #endregion

        #region Business Rules

        protected override void AddBusinessRules()
        {
            // TODO: add validation rules
            base.AddBusinessRules();

            //BusinessRules.AddRule(new Rule(IdProperty));
        }

        private static void AddObjectAuthorizationRules()
        {
            // TODO: add authorization rules
            //BusinessRules.AddRule(...);
        }

        #endregion

        #region Factory Methods

        public static CategoriaExpediente NewEditableRoot()
        {
            return DataPortal.Create<CategoriaExpediente>();
        }

        public static CategoriaExpediente GetEditableRoot(Guid id)
        {
            return DataPortal.Fetch<CategoriaExpediente>(id);
        }
        public static CategoriaExpediente GetEditableRoot(string categoriaId,string expedId)
        {
            CategoriaExpedienteCriteria criteria = CategoriaExpedienteCriteria.NewCategoriaExpedienteCriteria();
            criteria.CategoriaId  = categoriaId;
            criteria.ExpedienteId = expedId;
            return DataPortal.Fetch<CategoriaExpediente>(criteria);
        }
        public static void DeleteEditableRoot(int id)
        {
            DataPortal.Delete<CategoriaExpediente>(id);
        }

        private CategoriaExpediente()
        { /* Require use of factory methods */ }

        #endregion

        #region Data Access

        [RunLocal]
        protected override void DataPortal_Create()
        {
            // TODO: load default values
            // omit this override if you have no defaults to set
            base.DataPortal_Create();
        }
        //
        private void DataPortal_Fetch(CategoriaExpedienteCriteria criteria)
        {
            var accesoDatos = new DriveDataBase(TypeDataBase.PostgreSql);
            var parametros = new List<Parametro> { new Parametro("ec_cte_id", criteria.CategoriaId),
                 new Parametro("ec_exp_id", criteria.ExpedienteId)
            };
            DataSet datos = accesoDatos.EjecutarDataSet("\"OBT_CATEGORIA_EXP_CAT\"", parametros);
            if (datos.Tables[0].Rows.Count > 0)
            {
                LoadProperty(IdProperty, Guid.Parse(datos.Tables[0].Rows[0][0].ToString()));
                LoadProperty(ExpedienteIdProperty, datos.Tables[0].Rows[0][1].ToString());
                LoadProperty(CategoriaIdProperty, datos.Tables[0].Rows[0][2].ToString());
                LoadProperty(ItemIdProperty, datos.Tables[0].Rows[0][3].ToString());
                LoadProperty(CodigoProperty, datos.Tables[0].Rows[0][4].ToString());
                LoadProperty(NombreProperty, datos.Tables[0].Rows[0][5].ToString());
                LoadProperty(OtroProperty, datos.Tables[0].Rows[0][6].ToString());

            }
        }
        private void DataPortal_Fetch(Guid criteria)
        {
            var accesoDatos = new DriveDataBase(TypeDataBase.PostgreSql);
            var parametros = new List<Parametro> { new Parametro("ec_cae_id", criteria.ToString()) };
            DataSet datos = accesoDatos.EjecutarDataSet("\"OBT_CATEGORIA_EXP\"", parametros);
            if (datos.Tables[0].Rows.Count > 0)
            {
                LoadProperty(IdProperty, Guid.Parse(datos.Tables[0].Rows[0][0].ToString()));
                LoadProperty(ExpedienteIdProperty, datos.Tables[0].Rows[0][1].ToString());
                LoadProperty(CategoriaIdProperty, datos.Tables[0].Rows[0][2].ToString());
                LoadProperty(ItemIdProperty , datos.Tables[0].Rows[0][3].ToString());
                LoadProperty(CodigoProperty, datos.Tables[0].Rows[0][4].ToString());
                LoadProperty(NombreProperty, datos.Tables[0].Rows[0][5].ToString());
                LoadProperty(OtroProperty, datos.Tables[0].Rows[0][6].ToString());
                
            }
        }

        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Insert()
        {
            var accesoDatos = new DriveDataBase(TypeDataBase.PostgreSql);
            var parametros = new List<Parametro> { 
                new Parametro("ec_cae_id", ReadProperty(IdProperty)),
                new Parametro("ec_exp_id", ReadProperty(ExpedienteIdProperty)),
                new Parametro("ec_dae_codigo", ReadProperty(CodigoProperty)),
                new Parametro("ec_dae_valor_otro", ReadProperty(OtroProperty))
            };
            accesoDatos.EjecutarQuery("\"INS_CATEGORIA_EXP\"", parametros);
        }

       
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_DeleteSelf()
        {
            DataPortal_Delete(this.Id);
        }

        [Transactional(TransactionalTypes.TransactionScope)]
        private void DataPortal_Delete(Guid criteria)
        {
            var accesoDatos = new DriveDataBase(TypeDataBase.PostgreSql);
            var parametros = new List<Parametro> { 
                new Parametro("ec_cae_id", ReadProperty(IdProperty))
            };
            accesoDatos.EjecutarQuery("\"ELI_CATEGORIA_EXP\"", parametros);
        }

        #endregion
    }
}
